#pragma once
#ifndef PARSER_H_INCLUDED
#define PARSER_H_INCLUDED
#include <stdbool.h>
#define BUF_SIZE ((1<<12)-1)

enum server_type {
  SERVER_ROOT=0x0001,
  SERVER_PROXY_PASS=0x0002
};

struct conf_http_server_location {
  enum server_type type;
  char from[256]; // root or proxy_pass
  char map_to[256];
};

struct conf_http_server {
  int listen;
  char server_name[256];
  struct conf_http_server_location location; // root or proxy_pass
};

struct conf_http {
  struct conf_http_server *servers[8];
  int server_num;
};

struct conf_http *read_conf_file(const char *const path);
void parse(const char *const s, struct conf_http **ret);
void parse_http(const char *const bracel, const char *bracer, struct conf_http **ret);
void parse_http_server(const char *const bracel, const char *bracer, struct conf_http_server **ret);
bool delimit(const char *const l, const char *const r, const char **ret_l, const char **ret_r);
const char *read_line_till_semicolon(const char *s);
void conf_read_key_value();
void conf_read_http();
void simplify(char *const s);
int how_many_servers(const char *s);

#define init_http_server(http) http = NULL
#define destroy_http_server(http) do {\
    for(int i=0; i<http->server_num; ++i) {\
      free(http->servers[i]);\
    }\
    free(http);\
  }while(0)

#endif

